#version 450 compatibility








layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;


//layout(binding = 1, offset = 0) uniform atomic_uint m_PickCount;
layout(binding = 2) uniform Buf_GlobalParameter1 {
    mat4 m_ModelMatrix;
};
layout(binding = 3) uniform Buf_GlobalParameter2 {
    vec4 m_RaysNear;
    vec4 m_RaysFar;

    uvec2 m_Range;
};




layout(std430, binding = 5) buffer Buf_GlobalParameter4 {
    vec4 m_Vert[];
};
layout(std430, binding = 6) buffer Buf_GlobalParameter5 {
    uint m_Index[];
};
layout(std430, binding = 7) buffer Buf_GlobalParameter6 {
    float m_PickDis[];
};


void main() {
    
    uint id = gl_NumWorkGroups.x*gl_WorkGroupID.y + gl_WorkGroupID.x;
    uint index = id;
    id *= 2;

    
    if(id < m_Range.y) {
        id += m_Range.x;

        m_PickDis[index] = 10000.0;
        //return;
        vec4 vert1 = m_ModelMatrix * vec4(m_Vert[m_Index[id]].xyz, 1.0);
        vec4 vert2 = m_ModelMatrix * vec4(m_Vert[m_Index[id+1]].xyz, 1.0);
        //vec4 vert1 = vec4(m_Vert[m_Index[id]].xyz, 1.0);
        //vec4 vert2 = vec4(m_Vert[m_Index[id+1]].xyz, 1.0);

        vec3 u = ( (vert2) - (vert1) ).xyz;
        vec3 v = m_RaysFar.xyz - m_RaysNear.xyz;
        vec3 w = (vert1).xyz - m_RaysNear.xyz;

        float a = dot(u, u);
        float b = dot(u, v);
        float c = dot(v, v);
        float d = dot(u, w);
        float e = dot(v, w);
        float dt = a*c - b*b;

        float sd = dt;
        float td = dt;

        float sn = 0.0;
        float tn = 0.0;

        if (abs(td) < 0.00000001) {
            //平行
            sn = 0.0;
            sd = 1.0;

            tn = e;
            td = c;
        } else {
            sn = (b*e - c*d);
            tn = a*e - b*d;
            if(sn<0.0){
                //最近点在起点以外
                return;
                sn = 0.0;
                tn = e;
                td = c;
                
            } else if (sn > sd){
                //最近点在终点之外
                return;
                sn = sd;
                tn = e+b;
                td = c;
            }
        }

        if(tn<0.0) {
            //最近点在t起点以外
            tn = 0.0;
            if (-d < 0.0)       //按(公式2)计算，如果等号右边小于0，则sc也小于零，取sc=0
                sn = 0.0;
            else if (-d > a)    //按(公式2)计算，如果sc大于1，取sc=1
                sn = sd;
            else
            {
                sn = -d;
                sd = a;
            }
        } else if (tn > td) {
            tn = td;
            if ((-d + b) < 0.0)
                sn = 0.0;
            else if ((-d + b) > a)
                sn = sd;
            else {
                sn = (-d + b);
                sd = a;
            }
        }

        float sc = 0.0;
        float tc = 0.0;
        if (abs(sn) < 0.00000001)
            sc = 0.0;
        else
            sc = sn / sd;

        if (abs(tn) < 0.00000001)
            tc = 0.0;
        else
            tc = tn / td;

        vec3 dis = w + (u * sc) - (v * tc);
        m_PickDis[index] = dot(dis,dis);
        //m_PickDis[index] = vert2.y;
    }
    
}



